﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace predic.Web.CustomControl
{
    public partial class FieldsGrid : UserControl
    {
        public FieldsGrid()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(FieldsGrid_Loaded);
        }

        void FieldsGrid_Loaded(object sender, RoutedEventArgs e)
        {
            CreateControls();
        }

        private void CreateControls()
        {
            AddRowDefinitions();
            AddColumnDefinitions();

            foreach (var ctrl in GridRowsElements)
            {
                string label = (string)ctrl.GetValue(FieldsGridItem.LabelTextProperty);
                int row = (int)ctrl.GetValue(FieldsGridItem.RowProperty);
                int col = (int)ctrl.GetValue(FieldsGridItem.ColumnProperty);
                FieldsGridItemEnum type = (FieldsGridItemEnum)ctrl.GetValue(FieldsGridItem.ItemTypeProperty);

                switch (type)
                {
                    case FieldsGridItemEnum.Textbox:
                        AddTextbox();
                        break;

                    case FieldsGridItemEnum.Button:
                        AddButton();
                        break;
                }
            }
        }

        private void AddButton()
        {

        }

        private void AddTextbox()
        {

        }

        private void AddColumnDefinitions()
        {
            for (int i = 0; i < Columns * 2; i++)
            {
                ColumnDefinition colDef;

                if (i % 2 == 0)
                {
                    colDef = new ColumnDefinition();
                    colDef.Width = new GridLength(0, GridUnitType.Auto);
                }
                else
                {
                    colDef = new ColumnDefinition();
                    colDef.Width = new GridLength(0, GridUnitType.Star);
                }

                gridRoot.ColumnDefinitions.Add(colDef);
            }
        }
        private void AddRowDefinitions()
        {
            for (int i = 0; i < Rows; i++)
            {
                RowDefinition rowDef = new RowDefinition();
                rowDef.Height = new GridLength(35);

                gridRoot.RowDefinitions.Add(rowDef);
            }
        }

        #region [pd] Rows

        public int Rows
        {
            get { return (int)GetValue(RowsProperty); }
            set { SetValue(RowsProperty, value); }
        }
        public static readonly DependencyProperty RowsProperty =
            DependencyProperty.Register("Rows", typeof(int), typeof(FieldsGrid), new PropertyMetadata(5));
        #endregion

        #region [pd] Columns
        public int Columns
        {
            get { return (int)GetValue(ColumnsProperty); }
            set { SetValue(ColumnsProperty, value); }
        }
        public static readonly DependencyProperty ColumnsProperty =
            DependencyProperty.Register("Columns", typeof(int), typeof(FieldsGrid), new PropertyMetadata(2));
        #endregion

        #region [pd] GridRowsElements
        public List<FieldsGridItem> GridRowsElements
        {
            get { return (List<FieldsGridItem>)GetValue(GridRowsElementsProperty); }
            set { SetValue(GridRowsElementsProperty, value); }
        }
        public static readonly DependencyProperty GridRowsElementsProperty =
            DependencyProperty.Register("GridRowsElements", typeof(List<FieldsGridItem>), typeof(FieldsGrid),
            new PropertyMetadata(new List<FieldsGridItem>()));
        #endregion

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }
    }


    public enum FieldsGridItemEnum
    {
        Textbox,
        Button,

    }

    public partial class FieldsGridItem : DependencyObject
    {
        public FieldsGridItem()
        {

        }


        #region [pd] ItemType

        public FieldsGridItemEnum ItemType
        {
            get { return (FieldsGridItemEnum)GetValue(ItemTypeProperty); }
            set { SetValue(ItemTypeProperty, value); }
        }

        public static readonly DependencyProperty ItemTypeProperty =
            DependencyProperty.Register("ItemType", typeof(FieldsGridItemEnum), typeof(FieldsGridItem), new PropertyMetadata(FieldsGridItemEnum.Textbox));

        #endregion

        #region [pd] LabelText

        public string LabelText
        {
            get { return (string)GetValue(LabelTextProperty); }
            set { SetValue(LabelTextProperty, value); }
        }

        public static readonly DependencyProperty LabelTextProperty =
            DependencyProperty.Register("LabelText", typeof(string), typeof(FieldsGridItem), new PropertyMetadata("Label"));

        #endregion

        #region [pd] Row

        public int Row
        {
            get { return (int)GetValue(RowProperty); }
            set { SetValue(RowProperty, value); }
        }

        public static readonly DependencyProperty RowProperty =
            DependencyProperty.Register("Row", typeof(int), typeof(FieldsGridItem), new PropertyMetadata(0));

        #endregion

        #region [pd] Column

        public int Column
        {
            get { return (int)GetValue(ColumnProperty); }
            set { SetValue(ColumnProperty, value); }
        }

        public static readonly DependencyProperty ColumnProperty =
            DependencyProperty.Register("Column", typeof(int), typeof(FieldsGridItem), new PropertyMetadata(0));

        #endregion
    }
}
